package com.cskaoyan.service;

import com.cskaoyan.bean.BO.WxCouponBo;
import com.cskaoyan.bean.PO.*;
import com.cskaoyan.bean.VO.WxCouponVo;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketCouponMapper;
import com.cskaoyan.mapper.MarketCouponUserMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @ClassName WxCouponServiceImpl
 * @Description TODO
 * @Author ke
 * @Date 2022/8/18 9:21
 * @Version 1.0
 */
@Service
public class WxCouponServiceImpl implements WxCouponService {

    @Autowired
    MarketCouponUserMapper couponUserMapper;

    @Autowired
    MarketCouponMapper couponMapper;

    @Autowired
    MarketUserMapper userMapper;

    // 0未使用 1已使用 2已过期
    @Override
    public CommonData<WxCouponVo> myList(Integer status, Integer page, Integer limit) {

        MarketCouponUserExample example = new MarketCouponUserExample();

        MarketCouponUserExample.Criteria criteria = example.createCriteria();

        if (status == 0) {
            Short sta = 0;
            criteria.andStatusEqualTo(sta);
        }

        if (status == 1) {
            Short sta = 1;
            criteria.andStatusEqualTo(sta);
        }

        if (status == 2) {
            Short sta = 2;
            criteria.andStatusEqualTo(sta);
        }

        PageHelper.startPage(page, limit);

        List<MarketCouponUser> pos = couponUserMapper.selectByExample(example);

        List<WxCouponVo> vos = new LinkedList<>();

        for (MarketCouponUser po : pos) {
            Integer couponId = po.getCouponId();
            MarketCoupon coupon = couponMapper.selectByPrimaryKey(couponId);
            WxCouponVo vo = new WxCouponVo(false, couponId, null, coupon.getDiscount(), po.getEndTime(), po.getId(), coupon.getMin(), coupon.getName(), po.getStartTime(), coupon.getTag());
            vos.add(vo);
        }


        // pageInfo里面包含了页面的信息还有list的信息
        PageInfo pageInfo = new PageInfo(vos);//必须是原始的查询结果


        return CommonData.data(pageInfo);
    }

    @Override
    public int exchange(WxCouponBo bo) {

        MarketCouponExample example = new MarketCouponExample();

        MarketCouponExample.Criteria criteria = example.createCriteria();

        if (bo == null) {
            return -1;
        }
        criteria.andCodeEqualTo(bo.getCode());


        List<MarketCoupon> list = couponMapper.selectByExample(example);

        if (list == null) {
            return 0;
        }

        MarketCoupon po = list.get(0);


        MarketCouponUser marketCouponUser = new MarketCouponUser();

        // 拿到username
        String username = (String) SecurityUtils.getSubject().getSession().getAttribute("username");

        MarketUserExample userExample = new MarketUserExample();

        MarketUserExample.Criteria userExampleCriteria = userExample.createCriteria();

        userExampleCriteria.andUsernameEqualTo(username);

        List<MarketUser> marketUsers = userMapper.selectByExample(userExample);

        MarketUser marketUser = marketUsers.get(0);

        Integer userId = marketUser.getId();

        marketCouponUser.setUserId(userId);
        marketCouponUser.setCouponId(po.getId());
        marketCouponUser.setStatus(po.getStatus());
        // marketCouponUser.setUsedTime(null);
        marketCouponUser.setStartTime(po.getStartTime());
        marketCouponUser.setAddTime(po.getAddTime());
        marketCouponUser.setEndTime(po.getEndTime());
        // marketCouponUser.setOrderId(null);
        marketCouponUser.setUpdateTime(po.getUpdateTime());
        marketCouponUser.setDeleted(false);

        couponUserMapper.insertSelective(marketCouponUser);
        return 1;
    }

    /**
     * list
     *
     * @param info
     * @return
     */
    @Override
    public CommonData List(BasePageInfo info) {
        MarketCouponExample marketCouponExample = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = marketCouponExample.createCriteria();
        criteria.andStatusEqualTo((short) 0);
        PageHelper.startPage(info.getPage(), info.getLimit());
        List<MarketCoupon> marketCoupons = couponMapper.selectByExample(marketCouponExample);
        PageInfo<MarketCoupon> marketCouponPageInfo = new PageInfo<>(marketCoupons);
        return CommonData.data(marketCouponPageInfo);
    }

    @Override
    public int receiveById(Integer couponId, Integer userId) {
        // 先查找该用户有没有领取过该优惠券
        MarketCouponUserExample example = new MarketCouponUserExample();
        MarketCouponUserExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andCouponIdEqualTo(couponId);
        List<MarketCouponUser> marketCouponUsers = couponUserMapper.selectByExample(example);
        if (marketCouponUsers.size() > 0) {
            //领过
            return 2;
        }

        MarketCouponUser marketCouponUser = new MarketCouponUser();
        marketCouponUser.setCouponId(couponId);
        marketCouponUser.setUserId(userId);
        marketCouponUser.setStatus((short) 0);
        marketCouponUser.setAddTime(new Date());
        marketCouponUser.setUpdateTime(new Date());
        int i = couponUserMapper.insertSelective(marketCouponUser);
        if (i == 1) {
            // 成功领取
            return 0;
        }
        return 1;
    }
}
